﻿using MediatR;
using System.CJL.API.Application.Command.RawMaterialCommand;
using System.CJL.Domain;
using System.CJL.Domain.DTO;
using System.CJL.Infrstructre.Interface;

namespace System.CJL.API.Application.CommandHanlder.RawMaterialCommandHanler
{
    /// <summary>
    /// 原料显示
    /// </summary>
    public class RawMaterialShowCommandHandler : IRequestHandler<RawMaterialShowCommand, PageDTO<RawMaterial>>
    {
        IRawMaterialRepository _repository;
        /// <summary>
        /// 构造函数
        /// </summary>
        public RawMaterialShowCommandHandler(IRawMaterialRepository repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 原料显示
        /// </summary>
        public async Task<PageDTO<RawMaterial>> Handle(RawMaterialShowCommand request, CancellationToken cancellationToken)
        {
            var list = (await _repository.GetAll()).Where(x=>!x.IsDelete);
            if (!string.IsNullOrEmpty(request.RawMaterialNumbering))
            {
                list = list.Where(x => x.RawMaterialNumbering.Contains(request.RawMaterialNumbering)).ToList();
            }
            if (!string.IsNullOrEmpty(request.RawMaterialName))
            {
                list = list.Where(x => x.RawMaterialName.Contains(request.RawMaterialName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.RawMaterilStatus))
            {
                list = list.Where(x => x.RawMaterilStatus == request.RawMaterilStatus).ToList();
            }
            PageDTO<RawMaterial> result = new PageDTO<RawMaterial>();
            result.listcount = list.Count();
            result.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
            result.plist = list.OrderByDescending(x => x.AddTime).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize).ToList();
            return result;
        }
    }
}
